let App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataType: 'all',
        list: {rows: []},
        page: 1,
        hasMore: true,
        size:10
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.dataType = options.type || 'all';
        this.setData({dataType: this.data.dataType});
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _this = this;
        // 获取订单列表
        this.onPullDownRefresh();
    },

    /**
     * 获取订单列表
     */
    getOrderList: function () {
        if (!this.data.hasMore) {
            console.log("no more data")
            return;
        }
        let _this = this
        App._get('agent.order/lists', {
            page: this.data.page,
            size: this.data.size,
            dataType: this.data.dataType
        }, function (result) {
            console.log(result.data)
            if (result.data.list.rows.length < _this.data.size) {
                console.log("没有更多数据了")
                _this.setData({
                    'hasMore': false,
                    'isLoading': false,
                });
            }
            let rows = _this.data.list.rows.concat(result.data.list.rows);
            _this.setData({
                page: _this.data.page + 1,
                'list.rows': rows,
                'isLoading': false
            })
        });
    },

    /**
     * 切换标签
     */
    bindHeaderTap: function (e) {
        this.setData({dataType: e.target.dataset.type});
        this.onPullDownRefresh();
    },


    /**
     * 跳转订单详情页
     */
    detail: function (e) {
        let order_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../order/detail?order_id=' + order_id
        });
    },

    onPullDownRefresh() {
        //请求数据更新
        console.log('refresh')
        this.setData({page: 1, hasMore: true, list: {rows:[]}})
        this.getOrderList();
    },
    onReachBottom(event) {
        //请求更多数据
        console.log('load more')
        this.setData({
            page: this.data.page + 1
        });
        this.getOrderList();
    }

});